
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:stripe_terminal_usb/stripe_terminal_usb.dart';

import 'api.dart';

class StripeController extends GetxController {
  BuildContext? context;
  StripeTerminalUsb stripeTerminalUsb = StripeTerminalUsb();
  RxString onInitMsg = "".obs;
  RxString onPayMessage = "".obs;
  RxString connectReadersStatusMsg = "".obs;
  RxInt batteryLevel = 0.obs;
  @override
  void onInit() {
    super.onInit();
    stripeTerminalUsb.permissionAllowed();
    stripeTerminalUsb.onBatteryLevelCallBack = (e){
      batteryLevel.value  = (double.parse(e.toStringAsFixed(2)) * 100).toInt();
      print("+++++++++++++++++++++++$batteryLevel");
    };
    stripeTerminalUsb.connectReadersStatusBack = (e){

    };
    stripeTerminalUsb.connectReadersStatusBack = (e){
      if(e != null) {
        var status = e["status"];
        if(status == 'readerDisconnect') {
          connectReadersStatusMsg.value = "readerDisconnect " + e["msg"];
        }else if(status == 'connectionStatus'){
          connectReadersStatusMsg.value = 'connectionStatus ' + e["data"];
        }else if(status == 'connectionSuccess'){
          connectReadersStatusMsg.value = 'connectionSuccess ' + e["msg"];
        }else if(status == 'connectionError'){
          connectReadersStatusMsg.value = 'connectionError ' + e["msg"];
        }else if(status == 'discoverReadersSuccess'){
          connectReadersStatusMsg.value = 'discoverReadersSuccess ' + e["msg"];
        }else if(status == 'discoverReadersError'){
          connectReadersStatusMsg.value = 'discoverReadersError ' + e["msg"];
        }else if(status == 'onReaderReconnectStarted'){
          connectReadersStatusMsg.value = 'onReaderReconnectStarted ' + e["msg"];
        }else if(status == 'onReaderReconnectSucceeded'){
          connectReadersStatusMsg.value = 'onReaderReconnectSucceeded ' + e["msg"];
        }else if(status == 'onReaderReconnectFailed'){
          connectReadersStatusMsg.value = 'onReaderReconnectFailed  ' + e["msg"];
        }else if(status == 'tokenError') {
          onInitMsg.value = 'tokenError ' + e["msg"];
        }else if(status == 'tokenSucceed'){
          onInitMsg.value = 'tokenSucceed ' + e["msg"];
        }
      }
    };
  }

  Future<void> init(String localId,String tokenUrl) async{
    stripeTerminalUsb.init(localId,tokenUrl,"13123");
  }

}